<template>
  <div id='Goods'>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 搜索输入框区域 -->
      <div class="searchIpt">
        <el-input v-model="searchInput" class="input-with-select" clearable placeholder="请输入内容"
                  size="small" @clear="clearSearchInput" @keydown.enter.native="subSearch">
          <el-button slot="append" icon="el-icon-search" @click="subSearch"></el-button>
        </el-input>
        <el-button class="button-select" size="small" type="primary" @click="goAddPage">添加商品</el-button>
      </div>
      <!-- 数据列表区域 -->
      <el-table :data="goodList.goods" border stripe>
        <el-table-column label="#" type="index">
        </el-table-column>
        <el-table-column label="商品名称" prop="goods_name" width="450px">
        </el-table-column>
        <el-table-column label="商品重量" prop="goods_weight">
        </el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price">
        </el-table-column>
        <el-table-column label="商品数量" prop="goods_number">
        </el-table-column>
        <el-table-column label="创建时间">
          <!-- 创建时间插槽 -->
          <template slot-scope="scope">
            {{ scope.row.add_time | delTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip :enterable="false" content="编辑" effect="dark" placement="top">
              <!-- todo:修改商品信息功能的开发(自由开发) -->
              <el-button icon="el-icon-edit" size="mini" type="primary" @click="openEditDialog(scope.row.goods_id)">
              </el-button>
            </el-tooltip>
            <el-tooltip :enterable="false" content="删除" effect="dark" placement="top">
              <el-button icon="el-icon-delete" size="mini" type="danger" @click="openDelete(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <div class="pagination-block">
        <el-pagination :current-page="GoodsListParams.pagenum" :page-size="GoodsListParams.pagesize"
                       :page-sizes="[1, 2, 5, 8]" :total="goodList.total"
                       layout="total, sizes, prev, pager, next, jumper"
                       @size-change="handleSizeChange" @current-change="handleCurrentChange">
        </el-pagination>
      </div>
    </el-card>
    <!-- 编辑商品信息弹出框 -->
    <el-dialog :visible.sync="editDialog" title="提示">
      <!-- 内容区域 -->
      <el-form ref="editForm" :model="editDialogList" label-width="80px">
        <el-form-item label="商品名称">
          <el-input v-model="editDialogList.goods_name" disabled size="mini"></el-input>
        </el-form-item>
      </el-form>
      <!-- <span slot="footer">
        <el-button @click="editDialog = false">取 消</el-button>
        <el-button type="primary" @click="sureSubEditDialog">确 定</el-button>
      </span> -->
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Goods',
  created() {
    this.getGoodsList()
  },
  data() {
    return {
      // 商品数据列表
      goodList: [],
      // 搜索输入框
      searchInput: '',
      // 请求获取商品数据的参数数据
      GoodsListParams: {
        query: '',
        pagenum: 1,
        pagesize: 8
      },
      // 弹出框控制显示隐藏
      editDialog: false,
      // 弹出框相关数据
      editDialogList: {}
    }
  },
  methods: {
    // 获取商品列表
    async getGoodsList() {
      const { data, meta } = await this.$http.get('goods', { params: this.GoodsListParams })
      if (meta.status !== 200) return this.$message.error('商品数据列表获取失败')
      this.goodList = data
      console.log(this.goodList)
    },
    // 分页相关函数
    handleSizeChange(val) {
      this.GoodsListParams.pagesize = val
      this.GoodsListParams.pagenum = 1
      this.getGoodsList()
    },
    handleCurrentChange(val) {
      this.GoodsListParams.pagenum = val
      this.getGoodsList()
    },
    // 删除商品操作
    openDelete(val) {
      this.$confirm('此操作将永久删除该商品数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        console.log(val)
        const { meta } = await this.$http.delete(`goods/${val.goods_id}`)
        if (meta.status !== 200) return this.$message.error(meta.msg)
        this.getGoodsList()
        this.$message.success('删除成功')
      }).catch(() => {
        this.$message('已取消删除')
      })
    },
    // 提交搜索相关函数
    subSearch() {
      this.GoodsListParams.query = this.searchInput
      this.getGoodsList()
    },
    clearSearchInput() {
      this.GoodsListParams.query = ''
      this.getGoodsList()
    },
    // 打开弹出框相关函数
    async openEditDialog(id) {
      this.editDialog = true
      const { data, meta } = await this.$http.get(`goods/${id}`)
      if (meta.status !== 200) return this.$message.error('商品的相关数据获取失败')
      this.editDialogList = data
      console.log(this.editDialogList)
    },
    // 确认提交弹出框的数据
    // sureSubEditDialog() {
    //   this.editDialog = false
    //   console.log(this.editDialogList.goods_name)
    // },
    // 前往商品添加页面
    goAddPage() {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="less" scoped>
.searchIpt {
  width: 35%;
  display: flex;
  margin-bottom: 15px;
}

.input-with-select {
  background-color: #fff;
  margin-right: 30px;
}
</style>
